
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Vector;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

public class Util {

    public static String username;
    public static Font fontTitle = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_LARGE);
    public static Font fontContent = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_MEDIUM);
    public static Font fontMenu = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL);

    public static void drawTitle(String title, Graphics g, Canvas canvas) {
        g.setColor(0);
        g.fillRect(0, 0, canvas.getWidth(), fontTitle.getHeight());
        int widthTitle = fontTitle.stringWidth(title);
        g.setFont(fontTitle);
        g.setColor(0xffffff);
        g.drawString(title, (canvas.getWidth() - widthTitle) / 2, 0, 0);
    }

    public static void resetScreen(Graphics g, Canvas canvas) {
//        canvas.setFullScreenMode(true);
        g.setColor(0xffffff);
        g.fillRect(0, 0, canvas.getWidth(), canvas.getHeight());
    }

    public static void drawMenu(String[] left, String right, Graphics g, Canvas canvas) {
        g.setColor(0);
        g.fillRect(0, canvas.getHeight() - fontMenu.getHeight(), canvas.getWidth(), canvas.getHeight() - fontMenu.getHeight());
        g.setFont(fontMenu);
        g.setColor(0xffffff);
        if (left.length != 0) {
            if (left.length > 1) {
                g.drawString("Menu", 5, canvas.getHeight() - fontMenu.getHeight(), 0);
            } else {
                g.drawString(left[0], 5, canvas.getHeight() - fontMenu.getHeight(), 0);
            }
        }
        g.drawString(right, canvas.getWidth() - 5, canvas.getHeight() - fontMenu.getHeight(), Graphics.TOP | Graphics.RIGHT);
    }

    public static Image resizeImage(Image src, int screenWidth, int screenHeight) {
        int srcWidth = src.getWidth();
        int srcHeight = src.getHeight();
        Image tmp = Image.createImage(screenWidth, srcHeight);
        Graphics g = tmp.getGraphics();
        int ratio = (srcWidth << 16) / screenWidth;
        int pos = ratio / 2;

        //Horizontal Resize

        for (int x = 0; x < screenWidth; x++) {
            g.setClip(x, 0, 1, srcHeight);
            g.drawImage(src, x - (pos >> 16), 0, Graphics.LEFT | Graphics.TOP);
            pos += ratio;
        }

        Image resizedImage = Image.createImage(screenWidth, screenHeight);
        g = resizedImage.getGraphics();
        ratio = (srcHeight << 16) / screenHeight;
        pos = ratio / 2;

        //Vertical resize

        for (int y = 0; y < screenHeight; y++) {
            g.setClip(0, y, screenWidth, 1);
            g.drawImage(tmp, 0, y - (pos >> 16), Graphics.LEFT | Graphics.TOP);
            pos += ratio;
        }
        return resizedImage;

    }

    public static void drawDialog(Graphics g, String content, Canvas canvas) {
        Vector contents = new Vector();
        int padding = 20;
        while (fontContent.stringWidth(content) > canvas.getWidth() - padding) {
            int index = content.lastIndexOf(' ');
            while (fontContent.stringWidth(content.substring(0, index)) > canvas.getWidth() - padding) {
                index = content.lastIndexOf(' ', index - 1);
            }
            contents.addElement(content.substring(0, index));
            content = content.substring(index + 1);
        }
        contents.addElement(content);

        int y = canvas.getHeight() - contents.size() * fontContent.getHeight() - 5;
        y /= 2;
        g.setColor(0xffffff);
        g.fillRect(padding / 4, y, canvas.getWidth() - padding / 2, contents.size() * fontContent.getHeight() + 10);
        g.setColor(0);
        g.drawRect(padding / 4, y, canvas.getWidth() - padding / 2, contents.size() * fontContent.getHeight() + 10);
        y += 5;
        for (int i = 0; i < contents.size(); i++) {
            g.drawString(contents.elementAt(i).toString(), padding / 2, y, 0);
            y += fontContent.getHeight();
        }
    }

    public static byte[] ConvertImageToByteArray(Image image) {
        int width = image.getWidth();
        int height = image.getHeight();

        byte[] byteArray = new byte[width * height * 4];
        int[] rgbArray = new int[width * height];

        image.getRGB(rgbArray, 0, width, 0, 0, width, height);

        for (int i = 0; i < rgbArray.length * 4; i += 4) {
            byteArray[i + 0] = (byte) (rgbArray[i / 4] >> 24);
            byteArray[i + 1] = (byte) ((rgbArray[i / 4] >> 16) & 0x000000FF);
            byteArray[i + 2] = (byte) ((rgbArray[i / 4] >> 8) & 0x000000FF);
            byteArray[i + 3] = (byte) (rgbArray[i / 4] & 0x000000FF);
        }
        return byteArray;
    }

    public static byte[] getByteArray(Image img) {
        byte[] bytes = null;
        int width = img.getWidth();
        int height = img.getHeight();
        int[] pixels = new int[width * height];
        img.getRGB(pixels, 0, width, 0, 0, width, height);

        try {
            // convert int[] to byte[]  
            ByteArrayOutputStream bout = new ByteArrayOutputStream();
            DataOutputStream dout = new DataOutputStream(bout);
            // I suggest writing these first: you'll need them to convert back  
            dout.writeInt(width);
            dout.writeInt(height);
            // then the pixels  
            for (int i = 0; i < pixels.length; i++) {
                dout.writeInt(pixels[i]);
            }
            bytes = bout.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bytes;
    }

    public static Image RescaleImage(Image original, double XRatio, double YRatio) { // transparent image
        // the original width and height
        int originalWidth = original.getWidth();
        int originalHeight = original.getHeight();

        // the target width and height
        int newWidth = (int) (XRatio * originalWidth);
        int newHeight = (int) (YRatio * originalHeight);

        // create and fill the pixel array from the original image
        int[] rawInput = new int[originalHeight * originalWidth];
        original.getRGB(rawInput, 0, originalWidth, 0, 0, originalWidth, originalHeight);

        // pixel array for the target image
        int[] rawOutput = new int[newWidth * newHeight];


        // YD compensates for the x loop by subtracting the width back out
        int YD = (originalHeight / newHeight) * originalWidth - originalWidth;
        int YR = originalHeight % newHeight;
        int XD = originalWidth / newWidth;
        int XR = originalWidth % newWidth;
        int outOffset = 0;
        int inOffset = 0;

        for (int y = newHeight, YE = 0; y > 0; y--) {
            for (int x = newWidth, XE = 0; x > 0; x--) {
                rawOutput[outOffset++] = rawInput[inOffset];

                inOffset += XD;
                XE += XR;

                if (XE >= newWidth) {
                    XE -= newWidth;
                    inOffset++;
                }
            }
            inOffset += YD;
            YE += YR;

            if (YE >= newHeight) {
                YE -= newHeight;
                inOffset += originalWidth;
            }
        }
        rawInput = null;
        System.gc();
        return Image.createRGBImage(rawOutput, newWidth, newHeight, true);
    }

    public static Vector Split(String str, String str1) {
        Vector vector = new Vector();
        while (true) {
            if (str.indexOf(str1) == -1) {
                vector.addElement(str);
                break;
            }
            vector.addElement(str.substring(0, str.indexOf(str1)));
            str = str.substring(str.indexOf(str1) + 1);
        }
        return vector;
    }
}
